using System;
using System.IO;
using System.Threading;

//
// * Copyright (c) 2002-2009 "Neo Technology,"
// *     Network Engine for Objects in Lund AB [http://neotechnology.com]
// *
// * This file is part of Neo4j.
// * 
// * Neo4j is free software: you can redistribute it and/or modify
// * it under the terms of the GNU Affero General Public License as
// * published by the Free Software Foundation, either version 3 of the
// * License, or (at your option) any later version.
// * 
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// * GNU Affero General Public License for more details.
// * 
// * You should have received a copy of the GNU Affero General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
// 
namespace org.neo4j.kernel.impl.util
{


	using NotFoundException = org.neo4j.graphdb.NotFoundException;

	public class FileUtils
	{
		private static int WINDOWS_RETRY_COUNT = 3;

		public static bool DeleteFile(string fileName)
		{
		    try
		    {
                // Delete the file if it exists.
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
		        return true;
		    }
		    catch (Exception)
		    {
		        return false;
		    }
		}


        public static bool renameFile(string srcFile, string renameToFile)
		{
			if (!srcFile.exists(srcFile))
			{
				throw new NotFoundException("Source file[" + srcFile.Name + "] not found");
			}
			if (renameToFile.exists())
			{
				throw new NotFoundException("Source file[" + renameToFile.Name + "] not found");
			}
			int count = 0;
			bool renamed = false;
			do
			{
				renamed = srcFile.renameTo(renameToFile);
				if (!renamed)
				{
					count++;
					waitSome();
				}
			}
			while (!renamed && count <= WINDOWS_RETRY_COUNT);
			return renamed;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static void truncateFile(FileChannel fileChannel, long position) throws IOException
		public static void truncateFile(FileChannel fileChannel, long position)
		{
			int count = 0;
			bool success = false;
			IOException cause = null;
			do
			{
				count++;
				try
				{
					fileChannel.truncate(position);
					success = true;
				}
				catch (IOException e)
				{
					cause = e;
				}

			}
			while (!success && count <= WINDOWS_RETRY_COUNT);
			if (!success)
			{
				throw cause;
			}
		}

		private static void waitSome()
		{
			try
			{
				Thread.Sleep(500);
			}
			catch (InterruptedException ee)
			{
				Thread.interrupted();
			} // ok
			System.gc();
		}

		public static string fixSeparatorsInPath(string path)
		{
			string fileSeparator = System.getProperty("file.separator");
			if ("\\".Equals(fileSeparator))
			{
				path = path.Replace('/', '\\');
			}
			else if ("/".Equals(fileSeparator))
			{
				path = path.Replace('\\', '/');
			}
			return path;
		}
	}

}